<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class StoreBargain extends Model
{
    use HasFactory;
    protected $table = 'store_bargain';
    protected $guarded = [];
    public function StoreProduct()
    {
        return $this->belongsTo(StoreProduct::class,'product_id','id');
    }
    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'y-m-d H:i:s');
    }

    /**
     * 查询砍价商品
     * @param $id
     * @return mixed
     */
    public static function storeBargainFind($id)
    {
        return self::where('id',$id)->first();
    }


    /**
     * 修改
     * @param $id
     * @param $storeBargain
     * @return mixed
     */
    public static function storeBargainUpdate($id,$storeBargain){
        return self::where('id',$id)->update([
            'look'=>$storeBargain+1
        ]);
    }

    /**
     * 修改
     * @param $storeBargainUser
     * @param $storeBargainUsers
     * @return mixed
     */
    public static function storeBargainUpdates($storeBargainUser,$storeBargainUsers){
        return self::where('id',$storeBargainUser)->update([
            'look'=>$storeBargainUsers+1
        ]);
    }

    /**
     * 关联商品
     * @param $size
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public static function storeBargainWithStoreProduct($size){
        return self::with(['StoreProduct'])->paginate($size);
    }
}
